<template>
  <a-layout id="components-layout-demo-side" >
      <a-layout-sider
        v-model="collapsed"
        :trigger="null"
        :collapsible="true"
        width="240"
        :style="{ height: '100vh', position: 'fixed', left: 0 ,zIndex:200}">
        <div class="custom-title">
          <img src="../assets/images/logo.png" />
          <div v-show="!collapsed" class="title">
            <p style="font-size:18px">丘钛SSO门户平台</p>
          </div>
        </div>
        <div>
          <a-menu
            theme="dark"
            mode="inline"
            :defaultOpenKeys="defaultOpenKeys"
            :selectedKeys="selectedKeys"
          >
            <template v-for="(itParent) in menus" v-if="itParent.hidden !== true">
              <a-sub-menu v-if="itParent.children" :key="itParent.path">
              <span slot="title">
                <a-icon :type="itParent.meta.icon" />
                <span class="title_content">{{ itParent.meta.title }}</span>
              </span>
                <template v-for="(a) in itParent.children" v-if="a.hidden !== true">
                  <a-sub-menu v-if="a.children" :key="a.path">
                  <span slot="title">
                    <a-icon :type="a.meta.icon" v-if="a.meta.icon" />
                    <span class="title_content">{{ a.meta.title }}</span>
                  </span>
                    <a-menu-item v-for="(item) in a.children" :key="item.path" v-if="item.hidden !== true">
                      <router-link :to="item.path">
                        <a-icon :type="item.meta.icon" v-if="item.meta.icon" />
                        <span class="title_content">{{ item.meta.title }}</span>
                      </router-link>
                    </a-menu-item>
                  </a-sub-menu>
                  <a-menu-item v-else :key="a.path">
                    <router-link :to="a.path">
                      <a-icon :type="a.meta.icon" v-if="a.meta.icon" />
                      <span class="title_content">{{ a.meta.title }}</span>
                    </router-link>
                  </a-menu-item>
                </template>
              </a-sub-menu>

              <a-menu-item v-else :key="itParent.path">
                <router-link :to="itParent.path">
                  <a-icon :type="itParent.meta.icon" v-if="itParent.meta.icon" />
                  <span class="title_content">{{ itParent.meta.title }}</span>
                </router-link>
              </a-menu-item>
            </template>
          </a-menu>
        </div>
      </a-layout-sider>
      <a-layout class="main-menu-box">
        <a-layout-header
          style="background: #2c83e1; padding: 0;"
          :style="{ position: 'fixed', zIndex: 100, width: '100%' }">
          <right-content :handleFunction="switchCollapsed" :collapsed="collapsed"/>
        </a-layout-header>
        <a-layout-content :class="collapsed?'body_margin_unfold':'body_margin_collapsed'" style="background-color:#fff;">
          <multi-tab :collapsed="collapsed"/>
          <div style="margin-top: 40px;">
            <router-view :key="$route.path"/>
            <a-spin :spinning="spinning">
              <div id="app-qiankun"></div>
            </a-spin>
          </div>
        </a-layout-content>
      </a-layout>

  </a-layout>
</template>
<script>
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'
import startQiankun from '@/micro'
import RightContent from '@/components/GlobalHeader/RightContentTab'
import defaultSettings from '@/config/defaultSettings'
import MultiTab from '@/components/MultiTab'
import GlobalFooter from '@/components/GlobalFooter'
import Ads from '@/components/Other/CarbonAds'
import LogoSvg from '../assets/logo.svg?inline'

export default {
  name: 'BasicLayoutTab',
  components: {
    SettingDrawer,
    GlobalFooter,
    MultiTab,
    RightContent,
    LogoSvg,
    Ads
  },
  data () {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end
      isDev: process.env.NODE_ENV === 'development' || process.env.VUE_APP_PREVIEW === 'true',

      collapsed: false,
      menus: [],
      defaultOpenKeys: [],
      selectedKeys: [],
      spinning: false,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,

        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false
    }
  },

  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.addRouters
    })
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []

    this.selectedKeys = [this.$route.path]
    this.pushSelect(this.menus, this.$route.path)
  },
  watch: {
    $route: function (route) {
      this.selectedKeys = [route.path]
      this.pushSelect(this.menus, route.path)
    }
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
    // startQiankun(this.changeLoading)
  },
  methods: {
    i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      console.log('type', type, value)
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    },
    changeLoading (loading) {
      this.spinning = loading
    },
    switchCollapsed () {
      this.collapsed = !this.collapsed
    },
    pushSelect (menus, path) {
      for (let i = 0; i < menus.length; i++) {
        const a = menus[i].path
        if (a === path) {
          return a
        } else {
          if (menus[i].children) {
            const ret = this.pushSelect(menus[i].children, path)
            if (ret) {
              this.defaultOpenKeys = [menus[i].path, ...this.defaultOpenKeys]
              return menus[i].path
            }
          }
        }
      }
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";
.custom-title {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 64px;
  background-color: #2c83e1;

  img {
    width: 32px;
    height: 32px;
  }

  p {
    white-space: nowrap;
    line-height: 16px;
    margin: 0;
    color: #ffffff;
    font-weight: bold
  }

  .title {
    animation: 0.5s linear ease;
    margin-left: 10px;
  }
}

.body_margin_unfold {
  margin: 64px 0 0 80px;
}

.body_margin_collapsed {
  margin: 64px 0 0 240px;
}

.menu-container {
  overflow: auto;
  height: calc(100vh - 64px);
  background-color: #fff;
  /deep/ .ant-menu{
    height: 100%;
  }
}
.title_content{
  user-select: none;
}
#app-qiankun{
  //min-height: calc(100vh - 40px - 64px);
}

</style>
